import {settings} from "../../assets/settings";
import api from "../../api";
import { Message  } from 'element-ui';
import route from 'vue-router'
export default {
  state:{
    user:null
  },
  mutations: {
    // 登录
    login (state, data) {
      state.user = data;
      sessionStorage.setItem("user",JSON.stringify(state.user));
    },
    // 退出
    logout (state) {
      state.user = null;
      sessionStorage.removeItem("user");
    }
  },
  actions:{
    updatePassword(context,user){
      return new Promise((resolve,reject) => {
        api.updatePwd(user).then((response) => {
          Message({
            message: response.data.message,
            center: true,
            type:response.data.result?'success':'error'
          });
          resolve();
          if (response.data.result) {
            context.commit('login', response.data.object);
            resolve();
          }
        }).catch((error) => {
          reject();
          console.log(error);
        });
      });
    },
    logout(context){
      return new Promise((resolve,reject) => {
        api.logout().then((response) => {
          if (!response.data.result) {
            Message({
              message: response.data.message,
              center: true,
              type: 'error'
            });
          } else {
            context.commit('logout');
            resolve();
          }
        }).catch((error) => {
          reject();
          console.log(error);
        });
      });
    },
    login(context,user){
      return new Promise((resolve,reject) => {
        api.login(user).then((response) => {
          if (!response.data.result) {
            Message({
              message: response.data.message,
              center: true,
              type: 'error'
            });
          } else {
            context.commit('login', response.data.object);
            resolve();
          }
        }).catch((error) => {
          reject();
          console.log(error);
        });
      });
    }
  }
}
